##################################################################
# File : /lua/common/preload.lua
# Description : Resource preloading
# Author : GPG
##################################################################

local preloads = {}

local function GetBP(id)
    if type(id) == 'string' then
        return __blueprints[string.lower(id)]
    else
        return id
    end
end

local extra_anim_keys = { 'AnimationCapture', 'AnimationUpgrade' }

function PreloadUnitResources(unit)
    local bp = GetBP(unit)
    if not bp then return end
    LOG('Preloading resources for unit ' .. bp.BlueprintId)
    local display = bp.Display
    PreloadCharacterResources(display.Character)
    if display.ExtraCharacters then
        for k,v in display.ExtraCharacters do
            PreloadCharacterResources(v)
        end
    end
    PreloadMeshResources(display.MeshBlueprint)
    PreloadMeshResources(display.MeshBlueprintEvil)
    if display.ExtraMeshes then
        for k,v in display.ExtraMeshes do
            PreloadMeshResources(v)
        end
    end
    for i,key in extra_anim_keys do
        PreloadAnimation(display[key])
    end
end

function PreloadCharacterResources(char)
    local bp
    if type(char) == 'string' then
        bp = Characters[string.lower(char)]
    else
        bp = char
    end
    if not bp then return end
    LOG('  Preloading resources for character ' .. bp.Name)
    for k,v in bp.Meshes do
        PreloadMeshResources(v)
    end
    if bp.States then
        for k,state in bp.States do
            if state.Compositions then
                for k,comp in state.Compositions do
                    PreloadAnimation(comp.Animation)
                end
            end
        end
    end
end

function PreloadAnimation(anim)
    if anim then
        preloads[anim] = GetGrannyAnimationResource(anim)
    end
end

local mesh_keys = { 'MeshName' }
local texture_keys = { 'AlbedoName', 'NormalsName', 'SpecularName', 'LookupName', 'AdditiveName', 'MultiplicativeName' }

function PreloadMeshResources(mesh)
    local bp = GetBP(mesh)
    if not bp then return end
    LOG('    Preloading resources for mesh ' .. bp.BlueprintId)
    for index,lod in bp.LODs do
        for i,key in mesh_keys do
            PreloadGrannyMesh(lod[key])
        end
        for i,key in texture_keys do
            PreloadTexture(lod[key])
        end
    end
end

function PreloadGrannyMesh(mesh)
    if mesh and mesh != '' then
        preloads[mesh] = GetGrannyMeshResource(mesh)
    end
end

function PreloadTexture(texture)
    if texture and texture != '' then
        preloads[texture] = GetTextureResource(texture)
    end
end

function PreloadMapMesh(meshfile)
    # Can't actually preload these yet.
end

function PreloadMapEffect(efxfile)
    local table = {}
    doscript(efxfile, table)
    PreloadMapMesh(table.File)
    local mesh = table.Mesh
    for i,param in mesh.Parameters do
        local value = param.Value
        if type(value) == 'string' then
            PreloadTexture(value)
        end
    end
end